package com.unicair.hancher.disableapp;

import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.unicair.hancher.disableapp.list.MainAdapter;
import com.unicair.hancher.disableapp.mvp.MainPresenter;
import com.unicair.hancher.disableapp.utils.HancherLogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {
    private MainAdapter mAdapter;
    private MainPresenter mPresenter;
    private PackageManager mPackageManager;
    private Unbinder unBinder;
    @BindView(R.id.recycler_view)RecyclerView mRecyclerView;
    @BindView(R.id.toolbar)Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setBinder();
        setupView();
        setupData();
    }

    @Override
    protected void onDestroy() {
        setUnBinder();
        super.onDestroy();
    }

    private void setBinder() {
        unBinder = ButterKnife.bind(this);
        mPackageManager = getPackageManager();
        mPresenter = new MainPresenter(getApplicationContext(),mPackageManager);
        mPresenter.binder();
    }

    private void setUnBinder() {
        unBinder.unbind();
        mPresenter.unBinder();
    }

    private void setupView() {
        mAdapter = new MainAdapter(this,mPresenter,mPackageManager);
        mPresenter.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mToolbar.inflateMenu(R.menu.menu_main);
        mToolbar.setOnMenuItemClickListener(this);
    }

    private void setupData() {
        mPresenter.updateAllData();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        HancherLogUtil.shortToast(this,"menu item="+item.getTitle());
        switch (item.getItemId()){
            case R.id.menu_launcher_app:
                mPresenter.showAppType(MainPresenter.MENU_APP_LAUNCHER);
                return true;
            case R.id.menu_all_app:
                mPresenter.showAppType(MainPresenter.MENU_APP_ALL);
                return true;
            case R.id.menu_disable_app:
                mPresenter.showAppType(MainPresenter.MENU_APP_DISABLE);
                return true;
        }
        return false;
    }
}
